package com.bravo.mvpkotlin.ui.main

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.bravo.mvpkotlin.R
import com.bravo.mvpkotlin.base.AppComponent
import com.bravo.mvpkotlin.base.BaseActivity
import com.bravo.mvpkotlin.data.User
import com.bravo.mvpkotlin.ui.detail.DetailActivity
import com.bravo.mvpkotlin.ui.main.MainAdapter.OnItemClickListener
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.sdk25.coroutines.onClick
import javax.inject.Inject

class MainActivity : BaseActivity(), MainViews, OnItemClickListener {

    @Inject
    lateinit var persenter: MainPersenter

    @Inject
    lateinit var mainAdapter: MainAdapter

    override fun attachLayoutId(): Int = R.layout.activity_main

    override fun initViews() {
        mainRecyclerView.layoutManager = LinearLayoutManager(this)
        mainRecyclerView.adapter = mainAdapter
        mainAdapter.onItemClickListener = this
        mainSearch.onClick {
            var str = mainEdit.text.toString()
            persenter.getUser(str)
        }
    }

    override fun onItemClick(user: User) {
       var intent = intentFor<DetailActivity>("user" to user)
        startActivity(intent)
    }

    override fun setUpComponent(appComponent: AppComponent) {
        DaggerMainActivityComponent.builder().appComponent(appComponent).mainActivityModule(MainActivityModule(this)).build().inject(this)
    }

    override fun showUsers(users: List<User>) {
        stopLoading()
        mainAdapter?.updateDatas(users)
    }

}
